function DispatchmessageCtrl(){}
import { DispatchCtrlLib } from '../chromeLib/DispatchCtrlLib'
import {DispatchVideoLib} from '../chromeLib/DispatchVideoLib';
let VideoLibMsgVideoInitDivArray = new Array();
/**
 * 发送文件消息
 * @param   fileName              发送的文件名
 * @param   url                   附件地址
 * @param   obj                   发送对象的信息
 * @param   user_id               调度台的isp_user表里面的user_id，用于查询消息
 */

DispatchmessageCtrl.sendfile=function(fileName,url,obj,user_id){
  var MsgObj={
    MsgType : 5,
    SendType : 0,
    GroupID	: 0,
    GroupNumber : '',
    GroupName : '',
    UserID : 0,
    UserNumber : '',
    UserName : '',
    TempGroupID : '',
    UserNumberList : '',
    GroupNumberList : '',
    MsgString : fileName,
    NickName : obj.NickName,//补充个人昵称
    MsgAttachAddr : url,
    locDescription : '',
    latitudeType : 0,
    latitude : 0,
    longitudeType : 0,
    longitude : 0,
    LocDescription:'',
  };

  /*var str=MsgObj.MsgString;
  var suffix=str.substring(str.lastIndexOf(".")).toLowerCase();
  if(suffix==".png"||suffix==".jpg" || suffix==".gif"){
    MsgObj.MsgType=1;
  }else if(suffix==".mp3"){
    MsgObj.MsgType=2;
  }else if(suffix==".mp4" || suffix==".3gp"){
    MsgObj.MsgType=3;
  }*/
  obj.conversationId=parseInt(obj.conversationId);
  var MsgInfo={
    MsgString:MsgObj.MsgString,
    MsgAttachAddr:MsgObj.MsgAttachAddr,
    LatitudeType:MsgObj.latitudeType,
    Latitude:MsgObj.latitude,
    LongitudeType:MsgObj.longitudeType,
    Longitude:MsgObj.longitude,
    LocDescription:MsgObj.LocDescription,
    Duration:2//录音时长
  };
  //修改发送文件
  var str=MsgObj.MsgAttachAddr;
  var suffix=str.substring(str.lastIndexOf(".")).toLowerCase();
  if(suffix==".png"||suffix==".jpg" || suffix==".gif"){
    MsgObj.MsgType=1;
  }else if(suffix==".mp3"){
    MsgObj.MsgType=2;
    MsgInfo.Duration = obj.time;
  }else if(suffix==".mp4" || suffix==".3gp"){
    MsgObj.MsgType=3;
  }
  var TargetInfo={};
  if(obj.conversationType == 1){        //对象为终端
    MsgObj.UserId = obj.conversationId;
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 0;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName
    };
  }else if(obj.conversationType > 1){  //对象为群组
    MsgObj.GroupID = obj.conversationId;
    MsgObj.GroupNumber = obj.number;
    MsgObj.GroupName = obj.name;
    MsgObj.SendType = 1;
    TargetInfo={
      GroupID:MsgObj.GroupID,
      GroupNumber:MsgObj.GroupNumber,
      GroupName:MsgObj.GroupName,
      NickName:MsgObj.NickName//补充个人昵称
    };
  }else {
    MsgObj.UserId = obj.conversationId;
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 2;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName
    };
  }
  try{
    var rspOBJ=DispatchCtrlLib.MsgSend(MsgObj.SendType, TargetInfo, MsgObj.MsgType, MsgInfo);
    if(rspOBJ!== null){
      //处理聊天记录
      var params = {
        tempGroupId : obj.conversationId,
        gid : obj.conversationId,
        uid : obj.conversationId,
        did : user_id,
        mesObj:rspOBJ
      };
      if(obj.conversationType == 1){
        params.type = 3;
      }else if(obj.conversationType > 1){
        params.type = 0;
      }
      // DispatchmessageCtrl.getTalkContent(params);     //查询聊天记录，用于显示
      console.log("-----------==============")
      console.log(MsgInfo)
      var Msg = "dealmessageSendfile";
      DispatchMessage(Msg,params);                     //发送成功后界面变化
    }
  }
  catch (e) {
    alert(e.name+':'+e.message);
  }
};

/**
 * 查询聊天记录
 * type :0 组聊,3 单聊，6 临时组聊
 * tempGroupId : 临时组Id,
 * gid : 组Id,
 * uid
 * did
 */
DispatchmessageCtrl.getTalkContent=function(obj,times){
  times = times||1;
  obj.index = this.preIndex;
  $.ajax({
    url: 'pocMss/queryMsgAfterIndex',
    dataType:'json',
    contentType: 'application/json',//返回结果数据类型
    type : 'post',
    async : false,
    data: JSON.stringify(obj),
    success: function(result){
      console.log(result);
      var msgPre = result.obj.list;
      if(msgPre.length > 0){
        times = 6;
        Dispatchmessage.showMsg(msgPre);       //展示聊天记录，前端页面展示
      }
    }
  });
  if(times < 6){
    times++;
    setTimeout(function(){plateA.getTalkContent(obj,times);}, 100);
  }
}
/**
 * 发送图片消息
 * @param   url                   附件地址
 * @param   obj                   发送对象的信息
 * @param   user_id               调度台的isp_user表里面的user_id，用于查询消息
 */


DispatchmessageCtrl.sendPhoto=function(obj,url,user_id){
  var MsgObj={
    MsgType : 1,
    SendType : 0,
    GroupID	: 0,
    GroupNumber : '',
    GroupName : '',
    UserID : 0,
    UserNumber : '',
    UserName : '',
    TempGroupID : '',
    UserNumberList : '',
    GroupNumberList : '',
    MsgString : '',
    MsgAttachAddr : url,
    NickName : obj.NickName,//补充个人昵称
    locDescription : '',
    latitudeType : 0,
    latitude : 0,
    longitudeType : 0,
    longitude : 0,
    LocDescription:''
  };
  // obj.conversationId=parseInt(obj.conversationId);
  var MsgInfo={
    MsgString:MsgObj.MsgString,
    MsgAttachAddr:MsgObj.MsgAttachAddr,
    LatitudeType:MsgObj.latitudeType,
    Latitude:MsgObj.latitude,
    LongitudeType:MsgObj.longitudeType,
    Longitude:MsgObj.longitude,
    LocDescription:MsgObj.LocDescription
  };
  var TargetInfo={};
  if(obj.conversationType == 1){
    MsgObj.UserId = obj.conversationId;
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 0;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName,
      NickName:MsgObj.NickName//补充个人昵称
    };
  }else if(obj.conversationType > 1){
    MsgObj.GroupID = obj.conversationId;
    MsgObj.GroupNumber = obj.number;
    MsgObj.GroupName = obj.name;
    MsgObj.SendType = 1;
    TargetInfo={
      GroupID:MsgObj.GroupID,
      GroupNumber:MsgObj.GroupNumber,
      GroupName:MsgObj.GroupName,
      NickName:MsgObj.NickName//补充个人昵称
    };
  }else {
    MsgObj.UserId = obj.conversationId;
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 2;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName
    };
  }
  try{
    var rspOBJ=DispatchCtrlLib.MsgSend(MsgObj.SendType, TargetInfo, MsgObj.MsgType, MsgInfo);
    if(rspOBJ!== null){
      //发送成功，处理聊天记录
      let params = {
        tempGroupId : obj.conversationId,
        gid : obj.conversationId,
        uid : obj.conversationId,
        did : user_id,
        mesObj:rspOBJ
      };
      if(obj.conversationType == 1){
        params.type = 3;
      }else if(obj.conversationType > 1){
        params.type = 0;
      }
      // DispatchmessageCtrl.getTalkContent(params);     //查询聊天记录，用于显示

      let Msg = "dealmessageSendphoto";
      DispatchMessage(Msg,params);                     //发送成功后界面变化               //发送成功后界面变化
    }
  }
  catch (e) {
    alert(e.name+':'+e.message);
  }
};



/**
 * 发送文本消息
 * @param   msgstring             文本消息内容
 * @param   obj                   发送对象的信息
 * @param   user_id               调度台的isp_user表里面的user_id，用于查询消息
 */
DispatchmessageCtrl.sendMessage=function(obj,msgstring,user_id){
  let MsgObj={
    MsgType : 0,
    SendType : 0,
    GroupID	: 0,
    GroupNumber : '',
    GroupName : '',
    UserID : 0,
    UserNumber : '',
    UserName : '',
    TempGroupID : '',
    UserNumberList : '',
    GroupNumberList : '',
    MsgString : msgstring,
    MsgAttachAddr : '',
    locDescription : msgstring,
    NickName : obj.NickName,//补充个人昵称
    latitudeType : 0,
    latitude : 0,
    longitudeType : 0,
    longitude : 0
  };
  let MsgInfo={
    MsgString:MsgObj.MsgString,
    MsgAttachAddr:MsgObj.MsgAttachAddr,
    LatitudeType:MsgObj.latitudeType,
    Latitude:MsgObj.latitude,
    LongitudeType:MsgObj.longitudeType,
    Longitude:MsgObj.longitude,
    LocDescription:MsgObj.locDescription||""
  };
  let TargetInfo={};
  if(obj.conversationType == 1 ){
    MsgObj.UserId = Number(obj.conversationId);
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 0;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName
    };
  }else if(obj.conversationType >1){
    MsgObj.GroupID = Number(obj.conversationId);
    MsgObj.GroupNumber = obj.number;
    MsgObj.GroupName = obj.name;
    MsgObj.SendType = 1;
    TargetInfo={
      GroupID:MsgObj.GroupID,
      GroupNumber:MsgObj.GroupNumber,
      GroupName:MsgObj.GroupName,
      NickName:MsgObj.NickName//补充个人昵称
    };
  }else{
    MsgObj.UserId = Number(obj.conversationId);
    MsgObj.UserNumber = obj.number;
    MsgObj.UserName = obj.name;
    MsgObj.SendType = 2;
    TargetInfo={
      UserID:MsgObj.UserId,
      UserNumber:MsgObj.UserNumber,
      UserName:MsgObj.UserName
    };
  }
  try{

    let rspOBJ=DispatchCtrlLib.MsgSend(MsgObj.SendType, TargetInfo, MsgObj.MsgType, MsgInfo);
    if(rspOBJ!== null){
      //发送成功，处理聊天记录
      let params = {
        tempGroupId : obj.conversationId,
        gid : obj.conversationId,
        uid : obj.conversationId,
        did : user_id,
        mesObj:rspOBJ
      };
      if(obj.conversationType == 1){
        params.type = 3;
      }else if(obj.conversationType > 1){
        params.type = 0;
      }
      // DispatchmessageCtrl.getTalkContent(params);     //查询聊天记录，用于显示
      let Msg = "dealmessageinput";
      DispatchMessage(Msg,params);                     //发送成功后界面变化
    }
  }
  catch (e) {
    alert(e.name+':'+e.message);
  }
};


/**
 * 撤回消息
 * @param   msgInfo               消息内容
 * @param   obj                   发送对象的信息
 * @param   user_id               调度台的isp_user表里面的user_id，用于查询消息
 */


DispatchmessageCtrl.backMsg= function(MsgIndex, TargetType, TargetInfo){
  let rspOBJ=DispatchCtrlLib.MsgWithdraw (MsgIndex,TargetType,TargetInfo);
/*  if(rspOBJ!== null){
    Dispatchmessage.backMsg(TargetInfo);      //处理消息撤回后的界面
  }else{
    Dispatchmessage.backMsg(-1);              //失败后的处理,参数传入-1
  }*/
  return rspOBJ

};
/**
 * 视频通话框加载
 */

DispatchmessageCtrl.MsgVideoCanvasInit=function() {
  VideoLibMsgVideoInitDivArray[0] = new DispatchVideoLib();                     //加载远端div
  VideoLibMsgVideoInitDivArray[0].OnMsgVideoStatus = DispatchmessageCtrl.OnMsgVideoStatus;
  VideoLibMsgVideoInitDivArray[0].Init(document.getElementById('msgVideo_div'));
  VideoLibMsgVideoInitDivArray[1] = new DispatchVideoLib();                     //加载本地div
  VideoLibMsgVideoInitDivArray[1].OnMsgVideoStatus = DispatchmessageCtrl.OnMsgVideoStatus;
  VideoLibMsgVideoInitDivArray[1].Init(document.getElementById('msgVideoLocal_div'));
  return VideoLibMsgVideoInitDivArray;
}
DispatchmessageCtrl.OnMsgVideoStatus=function(event) {
  console.log("OnVideoStatus" + event);
}
DispatchmessageCtrl.MsgVoipSetCallVolume=function(CallID, Volume) {
  return DispatchCtrlLib.VoipSetCallVolume(CallID, Volume)
}
DispatchmessageCtrl.dealmsgVoipHandleIncomingCall=function(CallID, Handle) {
  return  DispatchCtrlLib.VoipHandleIncomingCall(CallID, Handle)
}

export { DispatchmessageCtrl }
